<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CommandTepairCharm extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $connect = 'api';

    protected $signature = 'CommandRepairCharm';

    protected $gift_stream = 'gift_stream';//礼物流水
    protected $finance_room_wealth_stream = 'finance_room_wealth_stream';//房间流水表

    
     /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '修复彩蛋魅力榜数据';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle(){
        $start = '2024-01-22 00:00:00';
        $end = '2024-01-28 23:59:59';

        $roomIds = DB::connection($this->connect)->table($this->gift_stream)
        ->select(DB::raw('room_id'))
        ->where('change_timestamp','>=',strtotime($start))
        ->where('change_timestamp','<=',strtotime($end))
        ->whereNotIn('room_id',['0','25355'])
        ->groupBy('room_id')
        ->get();
        $roomIds = objToArray($roomIds);
        foreach($roomIds as $r){
            $giftData = DB::connection($this->connect)->table($this->gift_stream)
            ->where('change_timestamp','>=',strtotime($start))
            ->where('change_timestamp','<=',strtotime($end))
            ->where('gift_price','!=',0)
            ->where('room_id',$r['room_id'])
            ->get();
            $giftData = objToArray($giftData);
            $room = DB::connection($this->connect)->table($this->finance_room_wealth_stream)
                ->where('room_id',$r['room_id'])
                ->where('create_time','>=',$start)
                ->where('create_time','<=',$end)
                ->get();
            $room = objToArray($room);
            foreach($giftData as $k=> $v){
                $price = $v['gift_price'] * $v['gift_num'];
                //更新房间流水
                if(in_array($v['gift_id'],['206','215','223'])){
                    $price = 0;
                }
                DB::connection($this->connect)->table($this->finance_room_wealth_stream)->where('id',$room[$k]['id'])->update(['wealth_value'=>$price]);
            }
        }
        
    }
}